<template>
  <el-card shadow="always" >
    <!---- -- -- -- 头部-- -- -- -- -- -- -->
    <div slot="header">
      <!-- 路径导航 -->
       <Breadcrumb/>
    </div>
     <!---- -- -- -- 内容区域-- -- -- -- -- -- -->
    <!-- 筛选区域 -->
    <div class="filter-box">
      <div class="lt">
        <el-form :model="queryForm" label-width="80px" :inline="true" size="normal">
          <el-form-item label="用户ID">
            <el-input v-model="queryForm.user_id"></el-input>
          </el-form-item>
          <el-form-item label="支付状态">
            <el-select v-model="queryForm.pay_status" >
              <el-option label="全部状态" value=""></el-option>
              <el-option label="已支付" value="1"></el-option>
              <el-option label="未支付" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否发货">
            <el-select v-model="queryForm.is_send" >
              <el-option label="全部状态" value=""></el-option>
              <el-option label="已发货" value="1"></el-option>
              <el-option label="未发货" value="0"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
            <el-button @click="onCancel">重置</el-button>
          </el-form-item>
        </el-form>

      </div>
      <div class="rt">
      </div>
    </div>
    <!-- 表格区域 -->
    <div class="table-box">
      <el-table :data="list" border>
        <el-table-column type="index" label="序号" width="100px"></el-table-column>
        <el-table-column prop="order_number" width="230px" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column prop="user_id" label="用户ID"></el-table-column>
        <el-table-column label="支付状态">
          <template slot-scope="scope">
            <el-tag type="info" v-if="scope.row.pay_status==0">未付款</el-tag>
            <el-tag type="success"  v-if="scope.row.pay_status==1">已付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="是否发货">
          <template slot-scope="scope">
            <el-button type="info" plain  size="small" icon="el-icon-close" circle v-if="scope.row.is_send=='否'"></el-button>
            <el-button type="success" plain size="small"  icon="el-icon-check" circle v-else></el-button>
          </template>
        </el-table-column>
        <el-table-column label="下单时间" width="180px">
          <template slot-scope="scope">
           {{ scope.row.create_time | formateTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改收货地址" placement="top">
              <el-button type="success" size="small"  @click="editAddressHandler(scope.row)"  icon="el-icon-edit" circle></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="查看物流信息" placement="top">
              <el-button type="warning"  size="small"   @click="seeExpressInfo(scope.row)" icon="el-icon-truck" circle ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <Pageination @sizechange="sizeChange"
                  @pagechange="currentChange"
                  :total="total"
                  :pagenum="pagenum"
                  :pagesize="pagesize"></Pageination>

    <!-- 准备修改收货地址的弹框 -->
    <el-dialog
      title="修改收货地址"
      :visible.sync="isShow"
      @close="resetAddressForm"
      width="45%">
      <!-- 表单内容 -->
      <el-form :model="addressForm"  label-width="80px" :inline="false" size="normal">
        <el-form-item label="省市县" required>
           <el-cascader
              v-model="addressForm.address"
              :options="cityOptions"
             :props="{checkStrictly:false}"
             clearable
            >
            </el-cascader>
            {{addressForm.address}}
        </el-form-item>
        <el-form-item label="详细地址" required>
          <el-input type="textarea" v-model="addressForm.detail_info"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="isShow = false">Cancel</el-button>
        <el-button type="primary" @click="editAddressSubmit">OK</el-button>
      </span>
    </el-dialog>

    <!-- 查看物流弹框 -->
    <el-dialog
      title="查看物流信息"
      :visible.sync="expressShow"
      width="60%">
      <!-- 物流进度 -->
      <el-row>
        <el-col :span="18" :offset="3">
          <el-timeline >
            <el-timeline-item
              v-for="(item, index) in expressList"
              :key="index"
              :icon="item.icon"
              :type="item.type"
              :color="item.color"
              :timestamp="item.time">
              {{item.context}}
            </el-timeline-item>
          </el-timeline>
        </el-col>
      </el-row>
      <span slot="footer">
        <el-button type="primary" @click="expressShow=false">关闭</el-button>
      </span>
    </el-dialog>

  </el-card>

</template>

<script>
import cityOptions from '@/utils/city_data2017_element.js'
const defaultItem = {
  user_id: '',
  pay_status: '',
  is_send: ''
}
export default {
  name: 'Order',
  data () {
    return {
      list: [],
      queryForm: { // 查询表单
        ...defaultItem
      },
      total: 0,
      pagenum: 1,
      pagesize: 10,
      isShow: false, // 修改收货地址
      addressForm: {
        address: '',
        detail_info: ''
      },
      cityOptions, // 省市县数据
      expressList: [], // 物流信息
      expressShow: false // 物流弹框
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取订单方法
    async getOrderList () {
      const { data: res } = await this.$axios.get('/orders', {
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize,
          ...this.queryForm
        }
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.list = res.data.goods
      this.total = res.data.total
    },
    // 查询确定
    onSubmit () {
      this.pagenum = 1
      this.pagesize = 10
      this.getOrderList()
    },
    // 重置事件
    onCancel () {
      this.queryForm = { ...defaultItem }
      this.pagenum = 1
      this.pagesize = 10
      this.getOrderList()
    },
    // 每页数量更换事件
    sizeChange (val) {
      this.pagesize = val
      this.getOrderList()
    },
    // 页码更换事件
    currentChange (val) {
      this.pagenum = val
      this.getOrderList()
    },
    // 修改收货地址提交事件
    editAddressSubmit () {
      console.log(this.addressForm)
      // 发送请求,修改指定id的收货地址数据
      this.$message.success('更新成功')
      this.isShow = false
    },
    // 重置表单
    resetAddressForm () {
      this.addressForm.address = this.addressForm.detail_info = ''
    },
    // 修改收货地址
    editAddressHandler (row) {
      // row这行的数据， 根据订单的id去查询该订单的收货地址，赋给addressFrom 然后再打开弹框
      this.addressForm.address = ['安徽省', '合肥市', '瑶海区']
      this.addressForm.detail_info = '闽商大厦黑马程序员武哥收'
      this.addressForm.id = 'xxx'
      this.isShow = true
    },
    // 查看订单物流信息
    async seeExpressInfo (row) {
      if (row.is_send === '否') {
        return this.$notify.warning({
          title: '警告',
          message: '暂未发货，没有物流信息',
          type: 'warning'
        })
      }
      // 根据订单id获取物流信息
      // const { data: res } = await this.$axios.get('/kuaidi/' + row.id)
      // const { data: res } = await this.$axios.get('/kuaidi/1106975712662')
      const res = {
        data: [
          {
            time: '2018-05-10 09:39:00',
            ftime: '2018-05-10 09:39:00',
            context: '已签收,感谢使用顺丰,期待再次为您服务',
            location: ''
          },
          {
            time: '2018-05-10 08:23:00',
            ftime: '2018-05-10 08:23:00',
            context: '[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件',
            location: ''
          },
          {
            time: '2018-05-10 07:32:00',
            ftime: '2018-05-10 07:32:00',
            context: '快件到达 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-10 02:03:00',
            ftime: '2018-05-10 02:03:00',
            context: '快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]',
            location: ''
          },
          {
            time: '2018-05-09 23:05:00',
            ftime: '2018-05-09 23:05:00',
            context: '快件到达 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 21:21:00',
            ftime: '2018-05-09 21:21:00',
            context: '快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]',
            location: ''
          },
          {
            time: '2018-05-09 13:07:00',
            ftime: '2018-05-09 13:07:00',
            context: '顺丰速运 已收取快件',
            location: ''
          },
          {
            time: '2018-05-09 12:25:03',
            ftime: '2018-05-09 12:25:03',
            context: '卖家发货',
            location: ''
          },
          {
            time: '2018-05-09 12:22:24',
            ftime: '2018-05-09 12:22:24',
            context: '您的订单将由HLA（北京海淀区清河中街店）门店安排发货。',
            location: ''
          },
          {
            time: '2018-05-08 21:36:04',
            ftime: '2018-05-08 21:36:04',
            context: '商品已经下单',
            location: ''
          }
        ],
        meta: { status: 200, message: '获取物流信息成功！' }
      }
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // 加工处理
      res.data[0].type = 'success'
      res.data[0].icon = 'el-icon-position'
      this.expressList = res.data
      this.expressShow = true
    }
  }
}
</script>

<style>

</style>
